define(function(require, exports){
	var $       = require("jquery");
	var util    = require('util');
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var utilDSP = require('modules/common');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': '',
				'isSubGrid': false,
				'hasHead': true, // 是否有头部标题
				'grid_config': null, // 列表配置
				'urlDelete': 'condition/remove',
				'view_model':{
					'view': {
						'addButton': true
					}
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			self.append($([
				'<div class="M-formHead">',
					'<span ms-visible="view.addButton" class="ml10" pub-name="addButton" pub-mod="@base/common/drop.add" ></span>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="admin/orientation/grid/grid.base"/>',
			].join('')));

			var config = {
				'addButton': {
					'def': LANG('新建定向策略'),
					'href': '#admin/orientation/edit',
					'href_target': '_self',
				},
				'grid': util.extend(c.grid_config, {
					'hasMenu': true,
					'hasBatch': true,
					'eventDataLoad': true,
					'hasSearch': true,
					'hasFilter': true,
					'hasFilterSidebar': true,
					'sidebar_config': {
						'height': 300,
						'items': [
							{'name': 'AdvertiserId', 'uri':'modules/sideBarBase.drop', 'config': {
								'title': LANG('广告主：'),
								'url': 'qualification/owns',
								'compareValue': true,
								'value': 0,
								'drop_config': {
									'name': 'Name',
									'key': '_id',
									// 'hasChild': true,
									'type': 'button',
									'width': 250,
									'hasSearch': true,
									'def': LANG('选择广告主'),
									'placeholder': LANG('选择广告主'),
									'value': 0,
									'all': {
										'_id': 0, 'Name': LANG('全部')
									},
									'panel_config': {
										'hasSearch': true,
										'canSelectParent': true
									}
								}
							}}
						]
					}
				})
			};
			self.buildFromTemplate(config);
		},
		// 操作列点击事件
		onOperateMenuShow: function( ev ) {
			var param = ev.param;
			var result = [];
			if (pubjs.checkRight('condition.update')) {
				result.push({'id': 'edit', 'name': LANG('编辑')});
			}

			if (pubjs.checkRight('condition.remove')) {
				result.push({'id': 'delete', 'name': LANG('删除')});
			}

			ev.returnValue = result;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function( ev ) {
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var name = ev.param[1]['Name'];
			var self = this;
			var c = self.getConfig();
			switch(key){
				case 'edit':
					pubjs.controller.navigate('admin/orientation/edit/' + id);
				break;
				case 'delete':
					pubjs.confirm(LANG('确认删除条件 %1 吗', name), function(state) {
						if(state)
						{
							self.showLoading();
							pubjs.mc.send(c.urlDelete, {'_id': +id}, function(err) {
								self.hideLoading();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.notify(LANG('删除成功'));
								self.reload();
							});
						}
					});
				break;
			}
		},
		reload: function(param){
			this.$.grid.setParam(param).load();
			return this;
		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'AdvertiserId':
						param.AdvertiserId = +id || '';
						break;
				}
			});

			self.reload(param);
		}
		// 批量操作-下拉菜单界面
		,onBatchShow: function(ev){
			// TODO: 根据权限展示弹出菜单
			// TODO: 根据选中内容展示弹出菜单
			var list = [];
			var batch = [
				{id: 'delete', name: LANG('删除'), right: 'remove'},
			];
			// 检查权限
			util.each(batch, function(item){
				if (pubjs.checkRight('condition.' + (item.right || item.id)))
				{
					list.push(item);
				}
			});
			ev.returnValue = list;
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var data = ev.param[0];
			var ids = ev.param[1];
			var key = data.key || '';
			var c = this.getConfig();
			if(!ids || !ids.length) {
				pubjs.alert(LANG('请先选择策略'));
				return false;
			}
			var self = this;
			switch(key){
				case 'delete':
					pubjs.confirm(LANG('确认删除以下定向策略 %1 吗', ids), function(state) {
						if(state)
						{
							self.showLoading();
							self.$.grid.setSelectRowIds([]);
							pubjs.mc.send(c.urlDelete, {'ids': ids}, function(err) {
								self.hideLoading();
								if(err)
								{
									return pubjs.alert(err.message);
								}
								pubjs.notify(LANG('删除成功'));
								self.reload();
							});
						}
					});
					break;
			}
			return false;
		},
	});
	exports.main = Main;
});